//
//  ProjectsTableViewController.m
//  song-git
//
//  Created by song on 16/2/22.
//  Copyright © 2016年 song. All rights reserved.
//

#import "ProjectsTableViewController.h"
#import "AFHTTPRequestOperationManager+Util.h"
#import "GLProject.h"


@interface ProjectsTableViewController ()

@property(nonatomic, assign) ProjectsType projectsType;
@property (nonatomic, copy) NSString *privateToken;
@property (nonatomic, assign) int64_t userID;
@property (nonatomic, assign) NSInteger page;

@end

@implementation ProjectsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _projects  = [NSMutableArray new];
//    [self fetchProject:YES];
    _projects = [[CacheProjectsUtil shareInstance] readProjectListWithProjectType:_projectsType];
    
}

-(id)initWithProjectsType:(ProjectsType)projectsType
{
    self =[super init];
    if(self)
    {
        _projectsType  =projectsType;
    }
    return  self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)fetchProject:(BOOL)refresh
{
    AFHTTPRequestOperationManager *manger =[ AFHTTPRequestOperationManager GitManager];
    NSString *strurl = [[self stringUrl] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [manger GET:strurl parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if ([responseObject count] == 0) {}
        else{
            [responseObject enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                GLProject *project = [[GLProject alloc] initWithJSON:obj];
                [_projects addObject:project];
            }];
            [[CacheProjectsUtil shareInstance] insertProjectList:_projects andProjectType:_projectsType];
            [self.tableView reloadData];
            
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 10;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  50;
}


- (NSString *)stringUrl
{
    NSMutableString *strUrl = [[NSMutableString alloc] initWithFormat:@"%@", GITAPI_HTTPS_PREFIX];
    
    switch (_projectsType) {
        case ProjectsTypeFeatured://0
        case ProjectsTypePopular://1
        case ProjectsTypeLatest://2
        {
            NSString *string = @[@"featured", @"popular", @"latest"][_projectsType];
            [strUrl appendFormat:@"%@/%@?",
             GITAPI_PROJECTS,
             string];
            
            break;
        }
        case ProjectsTypeStared://4
        case ProjectsTypeWatched://5
        {
            NSString *string = @[@"stared_projects", @"watched_projects"][_projectsType-4];
            [strUrl appendFormat:@"%@/%lld/%@?",
             GITAPI_USER,
             _userID,
             string];
            
            break;
        }
        case ProjectsTypeUserProjects://8
        {
            if (_privateToken.length) {
                [strUrl appendFormat:@"%@?private_token=%@&",
                 GITAPI_PROJECTS,
                 _privateToken];
            } else {
                [strUrl appendFormat:@"%@/%lld/%@?",
                 GITAPI_USER,
                 _userID,
                 GITAPI_PROJECTS];
            }
            
            break;
        }
        case ProjectsTypeLanguage://6
        {
            [strUrl appendFormat:@"%@/languages/%ld?",
             GITAPI_PROJECTS,
             (long)_languageID];
            break;
        }
        case ProjectsTypeSearch://7
        {
            [strUrl appendFormat:@"%@/search/%@?private_token=%@&",
             GITAPI_PROJECTS,
             _query,
             _privateToken];
            [self.tableView reloadData];
            break;
        }
        case ProjectsTypeEventForUser://3
        {
            [strUrl appendFormat:@"%@/%@/%lld?",
             GITAPI_EVENTS,
             GITAPI_USER,
             _userID];
            break;
        }
            
    }
    [strUrl appendFormat:@"page=%ld", (long)_page];
    
    return strUrl;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [UITableViewCell new];
    cell.textLabel.text = @"hello word ";
    
    return cell;
}


@end
